package v1

import (
	"github.com/gin-gonic/gin"
	"manager/bll"
	"manager/model"
	"manager/utils"
)

var Sms = &SmsRouter{}

func init() {
	// 注册路由
	RegisterRouter(Sms)
}

type SmsRouter struct {
}

func (s *SmsRouter) Init(r *gin.RouterGroup) {
	g := r.Group("/sms")
	{
		g.POST("/send", s.send)
		g.POST("/check", s.check)
	}
}

func (s *SmsRouter) send(c *gin.Context) {
	var (
		err error
		in  = &model.SmsSendCodeRequest{}
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	//调用短信服务
	err = bll.Sms.Send(c.Request.Context(), in)
	if err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

func (s *SmsRouter) check(c *gin.Context) {
	//检查验证码
	var (
		err error
		in  = &model.SmsCheckRequest{}
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	//调用短信服务
	err = bll.Sms.CheckCode(c.Request.Context(), in)
	if err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}
